name: Release

on: [create]

jobs:
  create_release:
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - name: Create release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: MCRPC ${{ github.ref }}
          draft: true
          prerelease: false
  build_and_upload_release_asset:
    name: Upload Release Asset
    needs: create_release
    runs-on: ${{ matrix.config.os }}
    strategy:
      fail-fast: false
      matrix:
        config:
          - { os: ubuntu-latest, asset: mcrpc-linux, path: mcrpc.bin }
          - { os: windows-latest, asset: mcrpc-windows.exe, path: mcrpc.exe }
          - { os: macos-latest, asset: mcrpc-macos, path: mcrpc.bin }
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Install python
        uses: actions/setup-python@v4
        with:
          python-version: '3.7'
          check-latest: true
          cache: 'pip'
      - name: Setup Poetry
        uses: pronovic/setup-poetry@v1
        with:
          version: "1.2.0"
          cache-venv: "true"
          cache-poetry: "true"
      - name: Set short version
        shell: bash
        run: |
          echo "VERSION=$(poetry version | sed -e 's/mcrpc //')" >> $GITHUB_ENV
      - name: Install python dependencies
        run: |
          poetry install --with dev
      - name: Build with poetry
        if: runner.os == 'Linux'
        run: |
          poetry build
      - name: Build with nuitka
        run: |
          poetry run python -m nuitka --assume-yes-for-downloads --follow-imports --enable-plugin=anti-bloat --onefile mcrpc.py
      - name: Upload release asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_name: ${{ matrix.config.asset }}
          asset_path: ${{ matrix.config.path }}
          asset_content_type: application/octet_stream
      - name: Upload release asset (Poetry tar.gz)
        if: runner.os == 'Linux'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_name: mcrpc-${{ env.VERSION }}.tar.gz
          asset_path: dist/mcrpc-${{ env.VERSION }}.tar.gz
          asset_content_type: application/gzip
      - name: Upload release asset (Poetry whl)
        if: runner.os == 'Linux'
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.create_release.outputs.upload_url }}
          asset_name: mcrpc-${{ env.VERSION }}-py3-none-any.whl
          asset_path: dist/mcrpc-${{ env.VERSION }}-py3-none-any.whl
          asset_content_type: application/zip
